package com.pinyougou.seller.controller;

import com.pinyougou.utils.FastDFSClient;
import com.pinyougou.utils.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class FileUploadController {
    @Value("${FILE_SERVER_URL}")
    private String FILE_SERVER_URL;

    @RequestMapping("/fileUpload")
    public Result FileUpload(MultipartFile file){
        try {
            //1、得到上传的文件名
            String filename = file.getOriginalFilename();
            System.out.println("上传的文件名："+filename);
            //2、得到上传文件的后缀名
            String suffix = filename.substring(filename.lastIndexOf(".") + 1);
            //3、创建文件上传的对象
            FastDFSClient client = new FastDFSClient("classpath:fdfs_client.conf");
            //4、开始上传文件，返回文件路径
            String url = client.uploadFile(file.getBytes(),suffix);
            url = FILE_SERVER_URL + url;
            System.out.println("文件路径："+url);
            //5、将上传成功后的URL地址发送发到前端页面
            return new Result(true,url);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"上传失败！");
        }
    }
}
